﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Baopx_API;

namespace Baopx.BackEnd.MangeAccount
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            grdAccount.DataSource = A.Controller.AccountController.GetAll();
            grdAccount.DataBind();
        }

        protected void ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    Account accountNew = new Account();
                    accountNew.UserRoleId =
                        Convert.ToInt32(((DropDownList) e.Item.FindControl("cboUserRole")).SelectedValue);
                    accountNew.UserName = ((TextBox)e.Item.FindControl("txtUserName")).Text;
                    accountNew.Password = ((TextBox)e.Item.FindControl("txtPassword")).Text;
                    accountNew.Email= ((TextBox)e.Item.FindControl("txtEmail")).Text;
                    accountNew.FirstName= ((TextBox)e.Item.FindControl("txtFirstName")).Text;
                    accountNew.LastName= ((TextBox)e.Item.FindControl("txtLastName")).Text;
                    accountNew.DateTimeCreate = DateTime.Now;
                    accountNew.IsActive = true;
                    A.Controller.AccountController.Insert(accountNew);
                    break;
                case "cmdUpdate":
                    Account accountUpdate =
                        A.Controller.AccountController.GetElementById(Convert.ToInt32(e.CommandArgument));
                    accountUpdate.UserName = ((TextBox)e.Item.FindControl("txtUserName")).Text;
                    accountUpdate.Password = ((TextBox)e.Item.FindControl("txtPassword")).Text;
                    accountUpdate.Email = ((TextBox)e.Item.FindControl("txtEmail")).Text;
                    accountUpdate.FirstName = ((TextBox)e.Item.FindControl("txtFirstName")).Text;
                    accountUpdate.LastName = ((TextBox)e.Item.FindControl("txtLastName")).Text;
                    accountUpdate.IsActive = ((CheckBox)e.Item.FindControl("chkActive")).Checked;
                    A.Controller.AccountController.Update();
                    break;
                case "cmdDelete":
                    Account accountDelete =
                        A.Controller.AccountController.GetElementById(Convert.ToInt32(e.CommandArgument));
                    A.Controller.AccountController.Delete(accountDelete);
                    break;
            }
            BindData();
        }

        protected void ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var cboUserRole = (DropDownList) e.Item.FindControl("cboUserRole");
                cboUserRole.DataSource = A.Controller.UserRoleController.GetAll();
                cboUserRole.DataTextField = "Name";
                cboUserRole.DataValueField = "Id";
                cboUserRole.DataBind();
                cboUserRole.SelectedValue = DataBinder.Eval(e.Item.DataItem, "UserRoleId").ToString();
            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                var cboUserRole = (DropDownList) e.Item.FindControl("cboUserRole");
                cboUserRole.DataSource = A.Controller.UserRoleController.GetAll();
                cboUserRole.DataTextField = "Name";
                cboUserRole.DataValueField = "Id";
                cboUserRole.DataBind();
            }
        }
    }
}